package com.lkd.controller;


import com.lkd.entity.Sku;
import com.lkd.service.SkuService;
import com.lkd.utils.AliOssUtil;
import com.lkd.vo.PageBean;
import com.lkd.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@Slf4j
@RestController
@RequestMapping("/api/vm-service/sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    //文件上传
    @Autowired
    private AliOssUtil aliOssUtil;

    //图片上传
    @PostMapping("/fileUpload")
    public Result upload(MultipartFile fileName) throws IOException {
        String path=aliOssUtil.upload(fileName.getOriginalFilename(),fileName.getInputStream());
        log.info("文件上传成功,路径为{}",path);
        return Result.success(path);
    }

    //新增商品
    @PostMapping
    public Boolean save(@RequestBody Sku sku){
        boolean save = skuService.save(sku);
        return save;
    }

    //分页查询
    @GetMapping("/search")
    public Result search(String skuName,
                         @RequestParam(defaultValue = "1")Integer pageIndex,
                         @RequestParam(defaultValue = "10")Integer pageSize){
        PageBean<Sku>page=skuService.search(skuName,pageIndex,pageSize);
        return Result.success(page);
    }

    //修改商品
    @PutMapping
    public Boolean update(@RequestBody Sku sku){
        boolean update = skuService.updateById(sku);
        return update;
    }

}
